﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace HBAClient
{
	public partial class ClientMain : Form
	{
		public ClientMain()
		{
			InitializeComponent();
		}

		private void CloseAllForms()
		{
			Form[] charr = this.MdiChildren;

			//for each child form, close it
			foreach (Form chform in charr)
				chform.Close();
		}

		private void ShowNewForm(object sender, EventArgs e)
		{
			//Bank bankForm = new Bank();
			//bankForm.MdiParent = this;
			//bankForm.Text = "Bank account management";
			//bankForm.Show();
			//

		}

		private void OpenFile(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
			if (openFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				string FileName = openFileDialog.FileName;
			}
		}

		private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
			saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
			if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
			{
				string FileName = saveFileDialog.FileName;
			}
		}

		private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
		{
			statusStrip.Visible = statusBarToolStripMenuItem.Checked;
		}

		private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.Cascade);
		}

		private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileVertical);
		}

		private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.TileHorizontal);
		}

		private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			LayoutMdi(MdiLayout.ArrangeIcons);
		}

		private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			foreach (Form childForm in MdiChildren)
			{
				childForm.Close();
			}
		}
		private void ClientMain_Load(object sender, EventArgs e)
		{
			this.WindowState = FormWindowState.Maximized;
			Dashboard formDashboard = new Dashboard();
			formDashboard.WindowState = FormWindowState.Maximized;
			formDashboard.MdiParent = this;
			formDashboard.Text = "Dashboard for you";

			// try if client exists in database
			if (Program.GetUserID() == 0)
			{
				MessageBox.Show("Cannot locate new user in database");
				this.Close();
			}
			try
			{
				formDashboard.Show();
			}
			catch (System.Data.SqlClient.SqlException)
			{
				MessageBox.Show("You cannot connect to HBA Database");
				this.Close();
			}

		}

		private void DashboardClick()
		{
			CloseAllForms();

			Dashboard formDashboard = new Dashboard();
			formDashboard.MdiParent = this;
			formDashboard.Text = "Dashboard for you";
			formDashboard.WindowState = FormWindowState.Maximized;
			formDashboard.Show();
		}
		private void MenuDashboard_Click(object sender, EventArgs e)
		{
			DashboardClick();
		}
		private void InClick()
		{
			CloseAllForms();

			In formIn = new In();
			formIn.MdiParent = this;
			formIn.Text = "In management";
			formIn.WindowState = FormWindowState.Maximized;
			formIn.Show();

		}
		private void MenuIn_Click(object sender, EventArgs e)
		{
			InClick();
		}

		private void OutClick()
		{
			CloseAllForms();

			Out formOut = new Out();
			formOut.MdiParent = this;
			formOut.Text = "Out management";
			formOut.WindowState = FormWindowState.Maximized;
			formOut.Show();
		}


		private void MenuOut_Click(object sender, EventArgs e)
		{
			OutClick();
		}

		private void ReportsClick()
		{
			CloseAllForms();

			Report formReports = new Report();
			formReports.MdiParent = this;
			formReports.Text = "Reports for you";
			formReports.WindowState = FormWindowState.Maximized;
			formReports.Show();
		}

		private void MenuReports_Click(object sender, EventArgs e)
		{
			ReportsClick();
		}

		private void MenuComboBanks_SelectedIndexChanged(object sender, EventArgs e)
		{
			CloseAllForms();

			Banks formBank = new Banks(MenuComboBanks.SelectedItem.ToString());
			formBank.MdiParent = this;
			formBank.Text = "Bank account management";
			formBank.Show();
			formBank.WindowState = FormWindowState.Maximized;

		}


		private void MenuComboBanks_Click(object sender, EventArgs e)
		{
			MenuComboBanks.Items.Clear();
			Util.SetBanks(ref MenuComboBanks);
		}

		private void MenuNewBank_Click(object sender, EventArgs e)
		{
            string strNewBankName = Util.DEFAULT_BANK_NAME;
            strNewBankName = Microsoft.VisualBasic.Interaction.InputBox("Please enter the new bank name and press OK. This bank will be added to your bank list. To cancel adding press Cancel", "New Bank Definition", Util.DEFAULT_BANK_NAME, 500, 300);

            if (strNewBankName.ToUpper() != Util.DEFAULT_BANK_NAME.ToUpper())
            {
                if (strNewBankName == "")
                {
                    // Do nothing. User pressed "Cancel"
                }
                else
                {
                    // Check if the save process ended successfully
                    if (Util.SaveNewBank(Program.GetUserID(), strNewBankName)) // Successfully
                    {
                        Util.SaveNewBalance(Program.GetUserID(), Util.GetNewBankID(strNewBankName), 0);
                    }
                    else // Failed
                    {
                        MessageBox.Show("The new bank adding is falied. Probably this bank name is already exist for you");
                    }
                }
            }
		}

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			InClick();
		}

		private void cmbBanksFront_Click(object sender, EventArgs e)
		{
			cmbBanksFront.Items.Clear();
			Util.SetBanks(ref cmbBanksFront);
		}

		private void cmbBanksFront_SelectedIndexChanged(object sender, EventArgs e)
		{
			CloseAllForms();


			Banks formBank = new Banks(cmbBanksFront.SelectedItem.ToString());
			formBank.MdiParent = this;
			formBank.Text = "Bank account management";
			formBank.Show();
			formBank.WindowState = FormWindowState.Maximized;

		}

		private void toolStripButton3_Click(object sender, EventArgs e)
		{
			OutClick();
		}

		private void tsDashFront_Click(object sender, EventArgs e)
		{
			DashboardClick();
		}

		private void tsReportsFront_Click(object sender, EventArgs e)
		{
			ReportsClick();
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{

		}

        private void MenuAbout_Click(object sender, EventArgs e)
        {
            MessageBox.Show("HBA Client" + Environment.NewLine + "AVIMARK Technologies 2009 ©");
        }
	}
}
